<?

/**
 * SimpleMailer
 * @package simplemailer
 * @author Alberto Pardo Saleme <apardo@adverit.com>
 * @update 3/6/2008 6:45:42 PM
 * Copyright (C) 2005 - AdVerit S.A. <www.adverit.com>
 *
 * <code>
 * // Config:
 * $GLOBALS["CONF"]['mail']['host'] = "smtp.gmail.com";
 * $GLOBALS["CONF"]['mail']['user'] = "mailbot@adverit.com";
 * $GLOBALS["CONF"]['mail']['pass'] = "mailbotadv";
 * $GLOBALS["CONF"]['mail']['method'] = "Swift"; // Swift, phpMailer, mail
 * $GLOBALS["CONF"]['mail']['ssl'] = true; // if using Google Apps or Gmail account, SSL is required
 *
 * // Sample code
 * $mailer = new SimpleMailer();
 * $mailer->to = "apardo@adverit.com";
 * $mailer->from = "webmaster@adverit.com";
 * $mailer->subject = "new email";
 * $mailer->body = "<html><body><b>Hello</b> world!</body></html>";
 * if ($mailer->send()) echo "Ok!";
 * else echo "Fail!";
 * </code>
 *
 */


class SimpleMailer
{
    /**
     * From; email
     * @access public
     * @var string
     */
    public $from = '';

    /**
     * error
     * @access public
     * @var string
     */
    public $error = '';

    /**
     * From Name
     * @access public
     * @var string
     */
    public $fromName = '';

    /**
     * To: email
     * @access public
     * @var string
     */
    public $to = '';
    /**
     * the subject
     * @access public
     * @var string
     */
    public $subject = '';
    /**
     * The body
     * @access public
     * @var string
     */
    public $body = '';
    /**
     * If some debug is needed
     * @access public
     * @var boolean
     */
    public $debug = false;
    /**
     * The method used to send the email
     * @access protected
     * @var string
     */
    protected $_method = 'mail';

    /**
     * Array containing the attachments
     * @access public
     * @var array
     *
     */
    public $arAttachments = array();
    
    /**
     * String containing the full path of the templates dir.
     * @access private
     * @var string
     */
    private $dirTemplates;

    /**
     * Constructor
     * $method is the method used to send the email.
     * @param string $method
     * @return integer
     */
    public function __construct($method = '')
    {
        $method = $method? $method : $GLOBALS["CONF"]['mail']['method'];
        $this->_method = $method;
        $this->dirTemplates = DIR_ROOT . "/templates";
    }

    /**
     * Sends the email. returns 1 if email sent, 0 if failure
     * @return integer
     */
    public function send()
    {
        $builder = new SimpleMailerBuilder();
        $engine = $builder->build($this->_method);
        $engine->from = $this->from;
        $engine->fromName = $this->fromName;
        $engine->to = $this->to;
        $engine->subject = $this->subject;
        $engine->body = $this->body;
        $engine->debug = $this->debug;
        $engine->arAttachments = $this->arAttachments;
        
        if ($engine->send()) return 1;
        else
        {
            $this->error = $engine->getErrormessage();
            return 0;
        }

    }
    /**
     * Attaches a file. returns 1 if attached, 0 if failure.
     * @return integer
     */
    public function addAttachment($path, $name =  NULL, $type = NULL)
    {
        if(!file_exists($path))
        {
            $this->_error("No se encontr&oacute; el achivo: ".$path);
            return 0;
        }

        // GET THE NAME OF THE FILE BY THE PATH.
        if(!$name)
        {
            $name = basename($path);
        }
        $attachment =  array();
        $attachment['path'] = $path;
        $attachment['name'] = $name;
        $attachment['type'] = $type;
        array_push($this->arAttachments,$attachment);
        return 1;
    }
    
    /**
     * Loads the email template.
     * It can be loaded with the full path or the filename
     */
    public function loadTemplate($template)
	{
	    if(strpos($template, $this->dirTemplates)===false)
	        $template = $this->dirTemplates ."/". $template;
	    	    
	    $this->body = file_get_contents($template);
	}
	
	/**
	 * Replaces the keys of the array given in the template body
	 */
	public function replaceText($data=array())
	{
	    if(empty($data)) return false;
	    
	    foreach($data as $key => $value)
	        $this->body = str_replace($key, $value, $this->body);
	}
}

?>
